#ifndef KUN_URP_WATER_FUNCTION_INCLUDED
#define KUN_URP_WATER_FUNCTION_INCLUDED


float3 ReconstructionWorldPosFromScreenPos(float2 screenPos, float depthTex)
{
    // float4 sceneWorldPos = mul(UNITY_MATRIX_I_VP, float4(screenPos * 2 - 1, depthTex, 1));
    // sceneWorldPos /= sceneWorldPos.w;
    // return sceneWorldPos.xyz; 
    float4x4 IVP = UNITY_MATRIX_I_VP;
    IVP._12_32 *= -1;
    IVP[1].xyz *= -1;
    float4 sceneWorldPos = mul(IVP, float4((screenPos * 2 - 1), depthTex, 1));
    sceneWorldPos /= sceneWorldPos.a;// homogeneous coordinates
    return GetAbsolutePositionWS(sceneWorldPos.xyz);// 从Camera-relative Space还原世界坐标
}


float3 ReconstructionWorldPosFromScreenPos(float2 screenPos)
{
    return ReconstructionWorldPosFromScreenPos(screenPos, SampleCameraDepth(screenPos));
}


half3 Scattering(half depth)
{
    return SAMPLE_TEXTURE2D(_WaterRampMap, sampler_WaterRampMap, half2(depth, 0.375h)).rgb;
}


half3 Absorption(half depth)
{
    return SAMPLE_TEXTURE2D(_WaterRampMap, sampler_WaterRampMap, half2(depth, 0.0h)).rgb;
}


float3 Foam(float depthDistance, float3 positionWS, float2 delta)
{
    float foamRange = 1 - pow(saturate(depthDistance * _FoamRange), 0.75);
    float foamNoise = SAMPLE_TEXTURE2D(_FoamNoiseMap, sampler_FoamNoiseMap, delta + TRANSFORM_TEX(positionWS.xz, _FoamNoiseMap)).r;
    foamNoise = PositivePow(foamNoise, _FoamNoisePower);
    float3 foam = step(foamNoise, foamRange);
    foam *= _FoamColor.rgb;
    return foam;
}


float3 Caustics(float3 sceneWorldPos, float3 sceneNormal, float2 delta)
{
    // float2 uv = sceneWorldPos.xz + sceneWorldPos.y * -(sceneNormal.x + sceneNormal.z) * (sceneNormal.y + 0.5) * 0.5;
    float2 uv = sceneWorldPos.xz;
    float2 uv1 = delta + TRANSFORM_TEX(uv, _CausticsMap);
    float3 result1 = SAMPLE_TEXTURE2D(_CausticsMap, sampler_CausticsMap, uv1).rgb;

    float2 uv2 = delta * float2(-1.07, -1.437) * 1.777 + TRANSFORM_TEX(uv, _CausticsMap);
    uv2 *= 0.777;
    float3 result2 = SAMPLE_TEXTURE2D(_CausticsMap, sampler_CausticsMap, uv2).rgb;

    return min(result1, result2) * 4;
}


float3 Fresnel(float3 normalWS, float3 viewDirectionWS)
{
    return pow(1.0 - saturate(dot(normalWS, viewDirectionWS)), _FresnelPower);
}


float3 SampleReflections(float3 normalWS, float3 viewDirectionWS, float fresnel)
{
    float3 reflectVector = reflect(-viewDirectionWS, normalWS);
    float3 reflection = SAMPLE_TEXTURECUBE(_CubemapTexture, sampler_CubemapTexture, reflectVector);
    return reflection * fresnel;
}


float3 Highlights(half roughness, half3 normalWS, half3 viewDirectionWS)
{
    Light mainLight = GetMainLight();

    half roughness2 = roughness * roughness;
    half3 halfDir = SafeNormalize(mainLight.direction + viewDirectionWS);
    half NoH = saturate(dot(normalize(normalWS), halfDir));
    half LoH = saturate(dot(mainLight.direction, halfDir));

    float d = NoH * NoH * (roughness2 - 1.h) + 1.0001h;
    float LoH2 = LoH * LoH;
    float specularTerm = roughness2 / ((d * d) * max(0.1h, LoH2) * (roughness + 0.5h) * 4);
    return specularTerm * mainLight.color * mainLight.distanceAttenuation;
}


#endif